Ext.define('cfWeb.view.appAudit.AppAuditView', {
    extend: 'Ext.container.Container',
    alias: 'widget.appAuditView',
    overflowY: 'auto',
    overflowX: 'hidden',
    items: [
        {
            xtype: 'collapsibleBlock',
            title: '待审批的应用',
            items: [
                {
                    xtype: 'grid',
                    store: Ext.create('cfWeb.store.appAudit.AppAuditStore'),
                    columns: [
                        {text: '编号', dataIndex: 'id', hidden: true},
                        {text: '模板编号', dataIndex: 'templateId', hidden: true},
                        {text: '时间', dataIndex: 'date', flex: 1},
                        {text: '内存', dataIndex: 'memory', flex: 1},
                        {text: '实例个数', dataIndex: 'instance', flex: 1},
                        {text: '空间', dataIndex: 'space', flex: 1},
                        {text: '组织', dataIndex: 'org', flex: 1},
                        {text: '申请人', dataIndex: 'userName', flex: 1},
                        {text: '理由', dataIndex: 'reason', flex: 1},
                        {
                            flex: 2,
                            text: '操作',
                            renderer: function (val, meta, rec) {
                                // generate unique id for an element
                                var id = Ext.id();
                                var id2 = Ext.id();
                                var store = this.getStore();
                                var state = rec.data.state;
                                Ext.defer(function () {
                                    if ((state == 0||state == 1)&&Ext.get(id)) {
                                        Ext.widget('button', {
                                            renderTo: id,
                                            text: '批准',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定批准' + rec.data.userName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/audit/agree?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }

                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                }
                                                            })
                                                        }
                                                    }
                                                });
                                            },

                                        });
                                    }
                                    if ((state == 0)&&Ext.get(id2)) {
                                        Ext.widget('button', {
                                            renderTo: id2,
                                            text: '拒绝',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定拒绝' + rec.data.userName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/audit/disagree?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }

                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                }
                                                            })
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                }, 100);
                                return Ext.String.format('<span id="{0}"></span><span id="{1}"></span>', id, id2);
                            }
                        }
                    ],
                    features: [{
                        ftype: 'grouping',
                        groupHeaderTpl: new Ext.XTemplate(
                            '<tpl if="name == 0">',
                            '未审核',
                            '</tpl>',
                            '<tpl if="name == 1">',
                            '未通过',
                            '</tpl>',
                            '<tpl if="name == 2">',
                            '已批准',
                            '</tpl>'
                        )
                    }],
                    listeners: {
                        render: function (me) {
                            var store = me.getStore();
                            //store.load();
                        }
                    }
                }
            ]
        }
    ]
});
